package com.weipt.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Scanner;

/**
 * @program: Learning
 * @description:
 * @Company: 汇华(北京)信息咨询服务有限公司
 * @author: peter.wei
 * @create: 2020-08-12 08:38
 **/
public class client1 {

    public static void main(String[] args) throws IOException {
        new client1().start();
    }

    public void start() throws IOException {
        /**
         * 链接服务器端
         */
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",8000));
        //向服务器端发送数据
        //从命令行获取数据，获取键盘的输入
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
            //获取这一行数据
            String request =  scanner.nextLine();
            //如果有数据，则发送，且数据不为空
            if(request != null && request.length() > 0){
                socketChannel.write(Charset.forName("UTF-8").encode(request));
            }
        }
    }
}
